//
//  CashHomeViewController.m
//  Consume100
//
//  Created by eliot on 16/3/24.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "CashHomeViewController.h"
#import "ImageTableViewCell.h"
#import "CashFirstTableViewCell.h"
#import "CashRecordTableViewCell.h"
#import "MakeCashTableViewController.h"
#import "ParticipationRecordsTableViewController.h"
#import "JoinMakeCashTableViewController.h"
#import "RulesForCashTableViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface CashHomeViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) UIScrollView *sc;
@property (nonatomic ,strong) UIPageControl *page;
/** 滚动图片 数组*/
@property (nonatomic , strong) NSArray *imgArr;
@property (nonatomic ,strong) NSTimer *timer;

@property (nonatomic, strong) NSDictionary *dataDic;

@property (nonatomic, strong) NSMutableArray *recordArr;
@end

@implementation CashHomeViewController

static int pindex = 1;

- (void)dealloc {
    pindex = 1;
}

-(NSMutableArray *)recordArr {
    if (!_recordArr) {
        _recordArr = [NSMutableArray array];
    }
    return _recordArr;

}

- (UIScrollView *)sc {
    if (!_sc) {
        _sc = [[UIScrollView alloc] init];
    }
    return _sc;
}


-(UIPageControl *)page {
    if (!_page) {
        _page = [[UIPageControl alloc] init];
        [self.tableView addSubview:_page];
        _page.frame = CGRectMake(0, 128 / 320.0 * Width - 20, Width, 20);
        [_page setAlpha:1];
    }
    return _page;
}


/** 设置滚动视图*/
- (void)setMyScrollView {
    self.sc.showsHorizontalScrollIndicator = NO;
    self.sc.frame = CGRectMake(0, 0, Width, 128 / 320.0 * Width);
    //创建页点
    [self createsCrollView:nil];
}


#pragma ------创建视图区域----------
//创建滚动视图区域
- (void)createsCrollView:(id)sender {
    //设置委托
    self.sc.delegate = self;
    //将是否取消对内容视图触摸的反应设为NO
    self.sc.canCancelContentTouches = NO;
    //设置滚动条类型
    // self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //是否可以缩放
    self.sc.scrollEnabled = YES;
    //是否可以进行页面切换
    self.sc.pagingEnabled = YES;
    NSUInteger page = 0;//为了记录页数
    int originX = 0;
    //填充滑动视图内容
    for (NSDictionary *imageDic in self.imgArr) {
        //首先创建一个视图
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectZero];
        imageview.contentMode = UIViewContentModeScaleToFill;
        //为这个图片设置适当位置
        CGRect rect ;
        rect.origin.x = originX;
        rect.origin.y = 0;
        rect.size.width = Width;
        rect.size.height = 128 / 320.0 * Width;
        imageview.frame = rect;
        //设置图片为背景
        [imageview sd_setImageWithURL:[NSURL URLWithString:imageDic[@"img"]]];
        //添加到视图中
        [self.sc addSubview:imageview];
        //确定下张图片开始的x坐标
        originX += Width;
        //记录页数
        page++;
    }
    //设置总页数
    self.page.numberOfPages = page;
    //设置默认页为首页
    self.page.currentPage = 0;
    self.page.tag = 100;
    //为页数控制关联方法
    [self.page addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    //显示适当区域
    [self.sc setContentSize:CGSizeMake(originX, 128 / 320.0 * Width)];
}
- (void)repartScroll {
    if (self.page.currentPage == self.imgArr.count - 1) {
        self.page.currentPage = 0;
        [self.sc setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else{
        self.page.currentPage = self.page.currentPage + 1;
    }
    CGRect rect;
    rect.origin.x = self.page.currentPage * self.sc.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = Width;
    rect.size.height = 128 / 320.0 * Width;
    //设置滚动视图的可视区域
    [self.sc scrollRectToVisible:rect animated:YES];
    
}

//点击小点切换页面
- (void)changePage:(id)sender {
    //创建一个区域
    CGRect rect;
    rect.origin.x = self.page.currentPage * self.sc.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = self.sc.frame.size.width;
    rect.size.height = self.sc.frame.size.height;
    //设置滚动视图的可视区域
    [self.sc scrollRectToVisible:rect animated:YES];
    
}

//重写滑动结束后的反应，就是把pageConteol的当前页跟随滑动而动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //取滚动视图区域的宽
    float orginX = self.sc.frame.size.width;
    //利用当前视图显示区域到view.frame的偏移量，来换算获取当前处在第几页
    int page = ((self.sc.contentOffset.x - orginX))/orginX +1;
    //当前页改为这一页。
    self.page.currentPage = page;
}



//页面将要进入前台，开启定时器
-(void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    //开启定时器
    [self.timer setFireDate:[NSDate distantPast]];
}

//页面消失，进入后台不显示该页面，关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
    //关闭定时器
    [self.timer setFireDate:[NSDate distantFuture]];
}

+(CashHomeViewController*)CreatCashHomeViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CashHomeViewController *goldRecord=[story instantiateViewControllerWithIdentifier:@"CashHomeViewController"];
    return goldRecord;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的补贴";
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    /** 定时器*/
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.75 target:self selector:@selector(repartScroll) userInfo:nil  repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"ImageTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CashFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"CashFirstTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CashRecordTableViewCell" bundle:nil] forCellReuseIdentifier:@"CashRecordTableViewCell"];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    self.tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    /** 设置导航啦样式*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    
    UserModel *moedl = userModel;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *dic = @{@"consumerId":moedl.id, @"page":@"1"};
    NSString *baseUrl = [NSString stringWithFormat:@"%@integralShopIndex.jhtml", MyBaseURL];
    [manager POST:baseUrl parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        dispatch_async(dispatch_get_main_queue(), ^{
            self.dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            self.imgArr = self.dataDic[@"bannersJson"];
            
            [self.recordArr addObjectsFromArray:self.dataDic[@"pagerCashRecord"]];
            
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"链接超时,请检查网络"];
        });
    }];

}

- (void)loadMoreData {
    pindex += 1;
    UserModel *moedl = userModel;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *dic = @{@"consumerId":moedl.id, @"page":[NSString stringWithFormat:@"%d", pindex]};
    NSString *baseUrl = [NSString stringWithFormat:@"%@integralShopIndex.jhtml", MyBaseURL];
    [manager POST:baseUrl parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            [self.tableView.mj_footer endRefreshing];
            [self.recordArr addObjectsFromArray:dic[@"pagerCashRecord"]];
            
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [self.tableView.mj_footer endRefreshing];
            [MBProgressHUD showError:@"链接超时,请检查网络"];
        });
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 4;
            break;
        case 2:
            return self.recordArr.count + 1;
            break;
        default:
            return 1;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.showsVerticalScrollIndicator = NO;
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imgCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"imgCell"];
        }
        //[self setMyScrollView];
        //[cell addSubview:self.sc];
        ImageTableViewCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"ImageTableViewCell" forIndexPath:indexPath];
        imageCell.selectionStyle = UITableViewCellSelectionStyleNone;
        imageCell.iconIV.image = [UIImage imageNamed:@"兑现"];
        imageCell.iconIV.contentMode = UIViewContentModeScaleAspectFill;
        imageCell.ivW.constant = Width;
        imageCell.ivH.constant = 128 / 320.0 * Width;

        return cell;;
    }else if (indexPath.section == 1){
        if (indexPath.row == 3) {
            CashFirstTableViewCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"CashFirstTableViewCell" forIndexPath:indexPath];
            firstCell.backGroundLb.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
            firstCell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            firstCell.selectionStyle =  UITableViewCellSelectionStyleNone;
            
           // NSLog(@"%d,%@, %@ ", [self.dataDic[@"numsJson"] intValue],self.dataDic[@"numJson"],self.dataDic[@"allNumJson"]);
            
            NSArray *arr1 = self.dataDic[@"numsJson"];
            NSArray *arr2 = self.dataDic[@"numJson"];
            NSArray *arr3 = self.dataDic[@"allNumJson"];
            NSArray *arr4 = self.dataDic[@"numNowJson"];
            
            firstCell.firstLb.text = [NSString stringWithFormat:@"%d", [arr1[0] intValue]];
            firstCell.secondLb.text = [NSString stringWithFormat:@"%d",[arr2[0] intValue]];
            firstCell.thridLb.text = [NSString stringWithFormat:@"%d",[arr3[0] intValue]];
            firstCell.fourthLb.text = [NSString stringWithFormat:@"%d",[arr4[0] intValue]];
            
            
            return firstCell;
        }else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            cell.selectionStyle =  UITableViewCellSelectionStyleNone;
            cell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            if (indexPath.row == 0) {
                 NSArray *arr = self.dataDic[@"coinJson"];
                cell.imageView.image = [UIImage imageNamed:@"icon_myGold"];
                cell.textLabel.text = [NSString stringWithFormat:@"我的金币  %d",[arr[0] intValue]];
                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                [cell addSubview:btn1];
                [btn1 setTitle:@"充值金币" forState:UIControlStateNormal];
                btn1.layer.cornerRadius = 8;
                btn1.layer.masksToBounds = YES;
                btn1.backgroundColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
                [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-10);
                    make.size.mas_equalTo(CGSizeMake(80, 30));
                    make.top.mas_equalTo(10);
                }];
            }else if (indexPath.row == 1){
                 NSArray *arr = self.dataDic[@"amountJson"];
                cell.imageView.image = [UIImage imageNamed:@"icin_tixian"];
                cell.textLabel.text = [NSString stringWithFormat:@"可提现金币  %d",[arr[0] intValue]];

                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                [cell addSubview:btn1];
                btn1.layer.cornerRadius = 8;
                btn1.layer.masksToBounds = YES;
                [btn1 setTitle:@"提现" forState:UIControlStateNormal];
                [btn1 addTarget:self action:@selector(clickMakeCashBtn) forControlEvents:UIControlEventTouchUpInside];
                btn1.backgroundColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
                [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(-10);
                    make.size.mas_equalTo(CGSizeMake(80, 30));
                    make.top.mas_equalTo(10);
                }];
            }else {
                cell.imageView.image = [UIImage imageNamed:@"icon_CYJL"];
                cell.textLabel.text = @"参与记录";
                UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
                iv.frame = CGRectMake(0, 0, 12, 12);
                iv.contentMode = UIViewContentModeScaleAspectFit;
                cell.accessoryView = iv;
            }
            return cell;
        }
        
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            cell.selectionStyle =  UITableViewCellSelectionStyleNone;
            UIView *sectionView = [[UIView alloc] init];
            [cell addSubview:sectionView];
            [sectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            sectionView.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] init];
            [sectionView addSubview:label];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"最近兑现记录";
            label.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(sectionView.center);
                make.size.mas_equalTo(CGSizeMake(112, 15));
            }];
            UIImageView *iv1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_line_L"]];
            iv1.contentMode = UIViewContentModeScaleAspectFit;
            [sectionView addSubview:iv1];
            [iv1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(2.5);
                make.bottom.mas_equalTo(-2.5);
                make.right.mas_equalTo(label.mas_left).mas_equalTo(-1);
                make.width.mas_equalTo(10);
            }];
            UIImageView *iv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_line_R"]];
            iv2.contentMode = UIViewContentModeScaleAspectFit;
            [sectionView addSubview:iv2];
            [iv2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(2.5);
                make.bottom.mas_equalTo(-2.5);
                make.left.mas_equalTo(label.mas_right).mas_equalTo(1);
                make.width.mas_equalTo(10);
            }];
            UILabel *bottomLb = [[UILabel alloc] init];
            [sectionView addSubview:bottomLb];
            bottomLb.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
            [bottomLb mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(1);
            }];
            return cell;
        }else {
            CashRecordTableViewCell *cashRecordCell = [tableView dequeueReusableCellWithIdentifier:@"CashRecordTableViewCell" forIndexPath:indexPath];
            cashRecordCell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            cashRecordCell.selectionStyle =  UITableViewCellSelectionStyleNone;
            NSDictionary *dic = self.recordArr[indexPath.row - 1];
            
            NSLog(@"%@", dic);
            cashRecordCell.phoneLb.text = dic[@"phone"];
            cashRecordCell.contentLb.text = [NSString stringWithFormat:@"已经获得%@元", dic[@"money"]];
            // 时间戳转时间的方法:
            long sendTime1 = [dic[@"createDate"][@"time"] longValue];
            sendTime1 = sendTime1 / 1000;
            NSDate *date1 = [CashHomeViewController timeFormatted:sendTime1];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            //用[NSDate date]可以获取系统当前时间
            NSString *timeSt1 = [dateFormatter stringFromDate:date1];
            timeSt1 = [timeSt1 stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
            cashRecordCell.timeLb.text = timeSt1;
            return cashRecordCell;
        }
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lastCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"lastCell"];
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cell addSubview:btn];
        btn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
        [btn setTitle:@"参与兑现" forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(-5);
        }];
        btn.layer.cornerRadius = 8;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(joinMakeCashBtn) forControlEvents:UIControlEventTouchUpInside];
        cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
        return cell;
    }
    
}
/** 时间处理*/
+ (NSDate *)timeFormatted:(long)totalSeconds
{
    
    
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return  128 / 320.0 * Width;
    }else if(indexPath.section == 1) {
        if (indexPath.row == 3) {
            return 121;
        }else {
            return 50;
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            return 35;
        }else {
            return 50;
        }
    }else {
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else {
        return 15;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    if (section == 3) {
//        return 20 ;
//    }else {
//        return 0;
//    }
//    
//    
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        RulesForCashTableViewController *vc = [RulesForCashTableViewController CreatRulesForCashTableViewController];
        [self creatNav:vc];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 2) {
            ParticipationRecordsTableViewController *vc = [ParticipationRecordsTableViewController CreatMakeCashHistoryTableViewController];
            [self creatNav:vc];
        }
    }

}

/** 点击参与提现*/
- (void)joinMakeCashBtn {
    JoinMakeCashTableViewController *vc = [JoinMakeCashTableViewController CreatMakeCashHistoryTableViewController];
    [self creatNav:vc];
    
}
/** 点击提现*/
- (void)clickMakeCashBtn {
    MakeCashTableViewController *vc = [MakeCashTableViewController CreatMakeCashTableViewController];
    [self creatNav:vc];
}



//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self.navigationController pushViewController:view animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)dismiss {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
